package com.qiyeji.real_estate.ProjectTask;

import com.qiyeji.real_estate.entity.TemporaryWorkKey;
import com.qiyeji.real_estate.entity.workKey;
import com.qiyeji.real_estate.service.WorkService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Repository
public class DeleteTemporaryWork implements ExecutionListener {

    @Autowired
    WorkService workService;

    @Autowired
    RuntimeService runtimeService;

    @Override
    public void notify(DelegateExecution execution) throws Exception {

        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(execution.getProcessInstanceId()).singleResult();
        String pCode = processInstance.getBusinessKey();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println(formatter.format(calendar.getTime()));


        System.out.println("插入数据到Work！");
        List<TemporaryWorkKey> temporaryWorkKeys = workService.fuzzyQuery1(pCode);
        for (TemporaryWorkKey temporaryWorkKey: temporaryWorkKeys)
            workService.insert(new workKey(temporaryWorkKey.getPcode(), temporaryWorkKey.getScode(), formatter.format(calendar.getTime())));
        System.out.println("清空TemporaryWork数据！");
        workService.deleteByPCode(pCode);
    }
}
